<?php
use app\models\Configs;

use app\models\Skeys;

use app\models\Users;

require '../bootstrap.php';
require '../models/Users.php';
require '../models/Configs.php';
require '../models/Skeys.php';

$user_id = intval(isset($_POST['user_id']) ? $_POST['user_id'] : 0);
$appkey = trim(isset($_POST['appkey']) ? $_POST['appkey'] : 0);

if($user_id > 0){
	$config = Configs::getConfigByAppkey($appkey);
	if(empty($config)){
		//没有找到该业务申请的记录
		echo  json_encode(array('code' => 2, 'message' => 'not allow'));
		exit();
	}
	$allow_user_types= explode(';', $config['type']);
	foreach ($allow_user_types as &$type){
		$type = trim($type);
	}
	$user = Users::getUserById($user_id);
	if(empty($user) || empty($config) || ! in_array(trim($user['type']), $allow_user_types)){
		//该用户没有权限
		echo  json_encode(array('code' => 1, 'message' => 'not allow'));
		exit();
	}
	//验证成功,生成skey
	if(empty($config)){
		echo json_encode(array('code' => -2));
		exit;
	}
	$skey = Skeys::saveSkey($user_id, $appkey);
	echo json_encode(array('code' => 0, 'skey' => $skey));
}else{
	//登录失败
	echo json_encode(array('code' => -1));
}

